CLIENT_DIR = web
SERVER_DIR = service
TEST_DIR = $(SERVER_DIR)/tests
TEST_INIT = $(TEST_DIR)/init.py
DATAIP_DIR = $(SERVER_DIR)/app/dataip
KS_PYPI_HOST = pypi.intra.knownsec.com
KS_PYPI = https://$(KS_PYPI_HOST)/testuser/dev
SHARE_SITE = http://prod-dev.intra.knownsec.com:8000
ZIP_FILE = cool_api-master.zip
DATAIPV4_FILE = dataipv4.ipdb
DATAIPV6_FILE = dataipv6.ipdb

export PYTHONPATH=$(SERVER_DIR)

.PHONY: all install test clean

all:
	make server_install
	make test
	make clean

install:
	make client_install
	make server_install

client_install:
	cd $(CLIENT_DIR) && yarn install
	cp ${CLIENT_DIR}/example.kuiper.config.js $(CLIENT_DIR)/kuiper.config.js
	patch -p1 < $(CLIENT_DIR)/kuiper.patch

server_install:
	# pip3 install --upgrade pip -i $(KS_PYPI) --trusted-host $(KS_PYPI_HOST)
	pip3 install -r $(SERVER_DIR)/requirements.txt -i $(KS_PYPI) --trusted-host $(KS_PYPI_HOST)
	pip3 install --force-reinstall --upgrade pycrypto==2.6.1 -i $(KS_PYPI) --trusted-host $(KS_PYPI_HOST)
	wget -q -c $(SHARE_SITE)/$(ZIP_FILE)  -O $(ZIP_FILE)
	wget -q -c $(SHARE_SITE)/$(DATAIPV4_FILE)  -O $(DATAIP_DIR)/$(DATAIPV4_FILE)
	wget -q -c $(SHARE_SITE)/$(DATAIPV6_FILE)  -O $(DATAIP_DIR)/$(DATAIPV6_FILE)
	pip3 install $(ZIP_FILE)
	cp $(SERVER_DIR)/app/config/settings.dev.yaml $(SERVER_DIR)/app/config/settings.yaml

client_run:
	cd $(CLIENT_DIR) && yarn start --host 0.0.0.0 --port 3000

server_run:
	cd $(SERVER_DIR) && python manage.py runserver --host 0.0.0.0 --port 4000 --debug

test:
	cp $(SERVER_DIR)/app/config/settings.test.yaml $(SERVER_DIR)/app/config/settings.yaml
	pip3 install coverage pytest pytest-cov -i $(KS_PYPI) --trusted-host $(KS_PYPI_HOST)
	python $(TEST_INIT)
	pytest --capture=no $(TEST_DIR)

clean:
	rm -rf .coverage .pytest_cache $(CLIENT_DIR)/node_modules $(SERVER_DIR)/app/config/settings.yaml $(ZIP_FILE)
	find $(SERVER_DIR) -name '*.pyc' | xargs rm -rf
	find $(SERVER_DIR) -name '__pycache__' | xargs rm -rf
